// pages/master/master.js
const app = getApp()
const util = require('../../utils/util.js')
const time = require('../../utils/time.js')
const Debug = require('../../utils/Debug.js')
Page({
        data: {
                id: '',//id相关视频
                pages:1,//第一页
                pageSize: 10,//每页5个
                name:"",//名家
                back: true,//返回按钮
                butshow:true,//展开状态
                list:[],//视频列表
                channel: "",//渠道
                namebox:'',//名家分享标题
                jutuanbox: '',//剧团分享标题
                share_user: "",//分享用户
                types:'1',//类型
        },
        onLoad: function(options) {
                var that=this
                if (options.types==1){
                        that.setData({
                                name:"名家",
                                typejt: 1,
                        })
                }else{
                        that.setData({
                                name: "剧团",
                                typejt:3,
                        })
                }
                that.setData({
                        id: options.id,
                        headerheight: app.globalData.navHeight,
                        types: options.types,
                        jutuanbox: ["@所有人，这里有《" + options.name + "》的新作品啦", "听说你喜欢《" + options.name + "》？来这里吧", "这里有《" + options.name + "》的所有视频！来听戏吧"],
                        namebox: ["@所有人，这里有秦腔名人《" + options.name + "》的新作品啦", "听说你喜欢《" + options.name + "》？来这里吧", "这里有《" + options.name + "》的所有视频！来听戏吧"],
                })
                this.request()
                // 判断入口
                if (!(options.user_id == undefined)) {
                        that.setData({
                                share_user: options.user_id
                        })
                }  
        },
        // 登录
        login: function () {
                var that = this
                wx.login({
                        success: function (res) {
                                var code = res.code
                                var data = {
                                        userCode: code,
                                        channel: that.data.channel,
                                        userImageUrl: "",
                                        userName: "",
                                        promoterId: that.data.share_user
                                }
                                util.promotion(app.globalData.appurl + "/qinqiang/user", "POST", data, that.succeed)
                        }
                })
        },
        succeed: function (res) {
                Debug.debug("<------- 登录------>")
                Debug.debug(res)
                wx.setStorageSync('tekon', res.data.token)
                wx.setStorageSync('userInfo', res.data.user)
                this.request()
        },
        onReachBottom: function() {
                var that = this
                var pages = that.data.pages
                pages++
                that.setData({
                        pages: pages
                })
                that.request()
        },
        onShareAppMessage: function (res) {
                var that = this
                if (that.data.types==1){
                      return {
                              title: that.data.namebox[Math.floor(Math.random() * that.data.namebox.length)],
                              path: 'pages/master/master?id=' + that.data.id + '&user_id=' + wx.getStorageSync("userInfo").id + '&types=' + that.data.types,
                              imageUrl: "https://img-1256481299.cos.ap-guangzhou.myqcloud.com/qqhxcx/jt.jpg",
                      }
              }else{
                      return {
                              title: that.data.jutuanbox[Math.floor(Math.random() * that.data.jutuanbox.length)],
                              path: 'pages/master/master?id=' + that.data.id + '&user_id=' + wx.getStorageSync("userInfo").id + '&types=' + that.data.types,
                              imageUrl: "https://img-1256481299.cos.ap-guangzhou.myqcloud.com/qqhxcx/jt.jpg",
                      }
              }
               
        },
        // 返回名家或者剧团
        backindex: function (e) {
                console.log(e.currentTarget.dataset.name)
                if (e.currentTarget.dataset.name=="名家"){
                         wx.reLaunch({
                                 url: '/pages/meet/meet?navactive=0',
                        })
                }else{
                        wx.reLaunch({
                                url: '/pages/meet/meet?navactive=1',
                        })
                }
                       
        },
        // 展开
        zhankai: function () {
                this.setData({
                        butshow: false,
                        actve: 0
                })
        },
        // 锁起来
        souqi: function () {
                this.setData({
                        butshow: true,
                        actve: 1
                })
        },
        request: function() {
                var that = this
                var id = that.data.id
                wx.showLoading({
                        title: '努力加载中...',
                })
                var data = {
                        pageIndex: that.data.pages,
                        pageSize: that.data.pageSize
                };
                var header = {
                        'authorization': wx.getStorageSync("tekon"),
                        'content-type': "application/x-www-form-urlencoded"
                }
                if (that.data.types == 1) { 
                        //名家详情
                        util.formhttp(app.globalData.appurl + '/qinqiang/famous/' + id, 'GET', data, header, that.porcessData);
                } else if (that.data.types == 2){
                        //剧团详情
                        util.formhttp(app.globalData.appurl + '/qinqiang/troupe/' + id, 'GET', data, header, that.porcessData);
                }
             
        },
        //列表成功的处理
        porcessData: function (res) {
                Debug.debug("<-------名家/剧团详情------>")
                Debug.debug(res)
                if (res.status == 401) {
                        this.login()
                }
                var that = this
                var pages = that.data.pages
                if (res.data.videoList == "" && pages == 1) {
                        that.setData({
                                none: "暂无视频，敬请期待",
                                classtip: 0,

                        })
                }
                if (res.data.videoList.length == 1 && pages == 1) {
                        that.setData({
                                none: "无更多内容",
                                classtip: 1,

                        })
                }
                if (res.data.videoList == "" && pages > 1) {
                        that.setData({
                                none: "无更多内容",
                                classtip: 1,

                        })
                }
                wx.hideLoading()
                var list = [];
                var totalList = [];
                for (var ids in res.data.videoList) {
                        var detail = res.data.videoList[ids];
                        var temp = {
                                id: detail.id,
                                title: detail.title,
                                imgUrl: detail.imgUrl,
                                hits: detail.hits,
                                bgUrl: detail.bgUrl,
                                duration: time.formatTime(new Date(detail.duration))
                        }
                        list.push(temp)
                }
                totalList = this.data.list.concat(list);
                if(that.data.types==1){
                        this.setData({
                                headers: res.data.famous,
                        })
                } else if (that.data.types == 2) {
                        this.setData({
                                headers: res.data.troupe,
                        })
                }
                this.setData({
                        list: totalList
                })
        }
})